<?php

class Mealtimes extends CI_Model {
    function __construct(){
        parent::__construct();
    }
    
    function getMealtime($mealtime_id){
        $this->db->where('mealtime_id', $mealtime_id);
        return $this->db->get("mealtimes");
    }
    
    function getMealtimes($year, $month){
        //Get the days in the month so we can select them all
        $endOfMonth = days_in_month($month, $year);
        
        //Going from day 1 to end of month
        $dateStart = date_to_mysql($year, $month, 1);
        $dateEnd = date_to_mysql($year, $month, $endOfMonth);
        
        $this->db->where('mealtime_date >=', $dateStart);
        $this->db->where('mealtime_date <=', $dateEnd);
        
        return $this->db->get("mealtimes");
    }
    
    function getMeals($id){
        $this->db->from('mealtime_meals AS MM');
        $this->db->join('meals AS M', 'MM.meal_id = M.meal_id');
        
        $this->db->where('MM.mealtime_id',$id);
        return $this->db->get();
    }
    
    function getMealID($year = null, $month = null, $day = null){
        if ($year == null){
            $year = date('Y');
        }
        if ($month == null){
            $month = date('n');
        }
        if ($day == null){
            $day = date('j');
        }
        $this->db->where('mealtime_date',  date_to_mysql($year, $month, $day));
        $query = $this->db->get("mealtimes");
        
        if ($query->num_rows() > 0){
            return $query->row();
        }
        else{
            return null;
        }
        return ;
    }
    
    function isOrderable($year = null, $month = null, $day = null){
        if ($year == null){
            $year = date('Y');
        }
        if ($month == null){
            $month = date('n');
        }
        if ($day == null){
            $day = date('j');
        }
        $this->db->where('mealtime_date',  date_to_mysql($year, $month, $day));
        $today = $this->db->get("mealtimes");
        
        if ($today->num_rows() == 0){
            $defaults = $this->db->get('defaults')->row();
            $day = date('L', mktime(0,0,0,$month, $day, $year));
            return $defaults['default_'.$day] == 1;
        }
        else{
            return $today->row()->mealtime_orderable == 1;
        }
    }
    
    function getMealsYMD($year = null, $month = null, $day = null){
        if ($year == null){
            $year = date('Y');
        }
        if ($month == null){
            $month = date('n');
        }
        if ($day == null){
            $day = date('j');
        }
        $this->db->from('mealtimes AS MT');
        $this->db->join('mealtime_meals AS MM', "MM.mealtime_id = MT.mealtime_id");
        $this->db->join('meals AS M', 'MM.meal_id = M.meal_id', "left");
                
        $this->db->where('MT.mealtime_date',  date_to_mysql($year, $month,$day));
        $this->db->select("M.*, MT.mealtime_id, MM.mealtime_meals_id, MT.mealtime_orderable, MT.mealtime_cutoff, MT.mealtime_servetime");
        return $this->db->get();
    }
    
    function create($year, $month, $day){
        $data['mealtime_date'] = date_to_mysql($year, $month, $day);
        $data['mealtime_servetime'] = time_to_mysql(18, 0);
        $data['mealtime_cutoff'] = time_to_mysql(20, 0);
        $data['mealtime_orderable'] = 1;
        $this->db->insert('mealtimes', $data);
        return $this->db->insert_id();
    }
    
    function hasMeals($mealtime_id){
        if ($mealtime_id <= 0){
            return false;
        }
        
        $this->db->where("mealtime_id", $mealtime_id);
        $this->db->limit(1);
        $rows = $this->db->get('mealtime_meals');
        if ($rows->num_rows() == 0){
            return false;
        }
        else{
            return true;
        }
    }
    
    function update($mealtime_id, $orderable, $cutoff, $servetime){
        $this->db->where("available_time_id", $cutoff);
        $cut = $this->db->get("available_times");
                
        $cutoff_time = $cut->row()->available_time_time;
        
        
        $this->db->where("available_time_id", $servetime);
        $serve = $this->db->get("available_times");
        
        $serve_time = $serve->row()->available_time_time;
        
        $this->db->trans_start();
        
        $this->db->set("mealtime_orderable", $orderable);
        $this->db->set("mealtime_cutoff",       $cutoff_time);
        $this->db->set("mealtime_servetime", $serve_time);
        $this->db->where("mealtime_id", $mealtime_id);
        $this->db->update("mealtimes");
        
        $this->db->trans_complete();
        return $this->db->trans_status();
    }
    
}